package com.xzg.argumentResolver;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xzg.annotation.XzgRequestParam;
import com.xzg.annotation.XzgService;

@XzgService("RequestParamArgumentResolver")
public class RequestParamArgumentResolver implements ArgumentResolver {

	public Boolean support(Class<?> type, int index, Method method) {
		// 先获取方法上所有的注解类
		Annotation[][] an = method.getParameterAnnotations();
		// 获取当前参数的注解
		Annotation[] paramAns = an[index];
		// 遍历当前参数的注解
		for (Annotation param : paramAns) {
			// 如果是XzgRequestParam
			if (XzgRequestParam.class.isAssignableFrom(param.getClass())) {
				return true;
			} else {
				continue;
			}
		}
		return false;
	}

	public Object argumentResolver(Class<?> type, int index, Method method, HttpServletRequest request,
			HttpServletResponse response) {
		// 先获取方法上所有的注解类
		Annotation[][] an = method.getParameterAnnotations();
		// 获取当前参数的注解
		Annotation[] paramAns = an[index];
		// 遍历当前参数的注解
		for (Annotation param : paramAns) {
			if (XzgRequestParam.class.isAssignableFrom(param.getClass())) {
				//转换成注解
				XzgRequestParam requestParam = (XzgRequestParam) param;
				//获取value
				String value = requestParam.value();
				//获取参数
				return request.getParameter(value);
			}
		}
		return null;
	}

}
